﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YJKExcelFormTool.Utilities
{
	public class LoopSearcher
	{
		public LoopSearcher(ExternalCommandData commandData, bool isSearchEdgeLoops)
		{
			this.m_Revit = commandData;
			this.m_IsSearchEdgeLoops = isSearchEdgeLoops;
		}

		public List<List<Curve>> GetEdgeLoopsByEdgeList(List<Curve> curveList)
		{
			List<List<Curve>> result = new List<List<Curve>>();
			List<MapEdge> edgeList = this.SegmentEdgeList(curveList);
			List<MapEdge> edgeList2 = this.FilterOverlapEdgeList(edgeList);
			List<MapNode> theNodes = new List<MapNode>();
			this.InitData(edgeList2, MapNode.SearchType._Anticlockwise, ref theNodes);
			List<List<MapEdge>> outEdgeList = new List<List<MapEdge>>();
			if (!this.SearchOuterBoundary(theNodes, ref outEdgeList))
			{
				return result;
			}
			List<List<MapEdge>> outEdgeList2 = new List<List<MapEdge>>();
			this.FormatEdgeLoops(outEdgeList, ref outEdgeList2);
			this.GetLoopCurves(outEdgeList2, false, ref result);
			return result;
		}

		public List<List<Curve>> GetCellsEdgeLoops(List<MapEdge> edgeList)
		{
			List<List<Curve>> result = new List<List<Curve>>();
			List<MapEdge> edgeList2 = this.SegmentEdgeList(edgeList);
			List<MapEdge> edgeList3 = this.FilterOverlapEdgeList(edgeList2);
			List<MapNode> theNodes = new List<MapNode>();
			this.InitData(edgeList3, MapNode.SearchType._Clockwise, ref theNodes);
			this.RecursiveDeleteSingleNode(ref theNodes);
			List<List<MapEdge>> outEdgeList = new List<List<MapEdge>>();
			if (!this.SearchCells(theNodes, ref outEdgeList))
			{
				return result;
			}
			this.GetLoopCurves(outEdgeList, true, ref result);
			return result;
		}

		private List<MapEdge> SegmentEdgeList(List<Curve> edgeList)
		{
			List<MapEdge> list = new List<MapEdge>();
			for (int i = 0; i < edgeList.Count; i++)
			{
				Curve curve = edgeList[i];
				XYZ hyendPoint = curve.GetEndPoint(0);
				XYZ ptStart = new XYZ(hyendPoint.X, hyendPoint.Y, 0.0);
				XYZ hyendPoint2 = curve.GetEndPoint(1);
				XYZ ptEnd = new XYZ(hyendPoint2.X, hyendPoint2.Y, 0.0);
				if (curve.GetType() == typeof(Arc))
				{
					Arc arc = curve as Arc;
					XYZ center = arc.Center;
					XYZ ptCenter = new XYZ(center.X, center.Y, 0.0);
					MapEdge item = MapEdge.NewEdge(new ElementId(i), ptStart, ptEnd, ptCenter, arc.Normal, 0.0, 0.0);
					list.Add(item);
				}
				else if (curve.GetType() == typeof(Line))
				{
					MapEdge item2 = MapEdge.NewEdge(new ElementId(i), ptStart, ptEnd, 0.0, 0.0);
					list.Add(item2);
				}
			}
			return list;
		}

		private List<MapEdge> SegmentEdgeList(List<MapEdge> edgeList)
		{
			List<MapEdge> list = new List<MapEdge>();
			List<List<XYZ>> list2 = new List<List<XYZ>>();
			List<Curve> list3 = new List<Curve>();
			foreach (MapEdge mapEdge in edgeList)
			{
				list2.Add(new List<XYZ>
				{
					mapEdge.StartPoint,
					mapEdge.EndPoint
				});
				XYZ startPoint = mapEdge.StartPoint;
				XYZ endPoint = mapEdge.EndPoint;
				Curve item;
				if (mapEdge.IsArcEdge)
				{
					XYZ center = mapEdge.Center;
					XYZ radianPnt = Geometry.CalculatMidPoint(startPoint, endPoint, center, XYZ.BasisZ);
					item = this.m_Revit.Application.ActiveUIDocument.Document.CreatYJKArc(startPoint, endPoint, radianPnt);
				}
				else
				{
					item = YJKLineEx.YJKGetBound(startPoint, endPoint);
				}
				list3.Add(item);
			}
			for (int i = 0; i < list3.Count; i++)
			{
				Curve curve = list3[i];
				for (int j = i + 1; j < list3.Count; j++)
				{
					Curve curve2 = list3[j];
					IntersectionResultArray intersectionResultArray;
                    if ((int)curve.Intersect(curve2, out intersectionResultArray) == 8)
					{
						foreach (object obj in intersectionResultArray)
						{
							IntersectionResult intersectionResult = (IntersectionResult)obj;
							if (intersectionResult.XYZPoint != null)
							{
								list2[i].Add(intersectionResult.XYZPoint);
								list2[j].Add(intersectionResult.XYZPoint);
							}
						}
					}
				}
			}
			for (int i = 0; i < edgeList.Count; i++)
			{
				MapEdge mapEdge2 = edgeList[i];
				Curve curve3 = list3[i];
				List<XYZ> intersectPoints = list2[i];
				List<XYZ> list4 = Common.UniqueAndSortIntersectPoints(curve3, intersectPoints);
				if (list4.Count >= 2)
				{
					if (list4.Count == 2)
					{
						list.Add(mapEdge2);
					}
					else
					{
						double positiveHalfWidth = mapEdge2.PositiveHalfWidth;
						double negativeHalfWidth = mapEdge2.NegativeHalfWidth;
						XYZ center2 = mapEdge2.Center;
						for (int k = 0; k < list4.Count - 1; k++)
						{
							XYZ xyz = list4[k];
							XYZ xyz2 = list4[k + 1];
							if (!Geometry.LessThan(xyz.DistanceTo(xyz2), 0.0001))
							{
								if (mapEdge2.IsArcEdge)
								{
									MapEdge item2 = MapEdge.NewEdge(mapEdge2.ComponentId, xyz, xyz2, center2, XYZ.BasisZ, positiveHalfWidth, negativeHalfWidth);
									list.Add(item2);
								}
								else
								{
									MapEdge item3 = MapEdge.NewEdge(mapEdge2.ComponentId, xyz, xyz2, positiveHalfWidth, negativeHalfWidth);
									list.Add(item3);
								}
							}
						}
					}
				}
			}
			return list;
		}

		private List<MapEdge> FilterOverlapEdgeList(List<MapEdge> edgeList)
		{
			new List<MapEdge>();
			return edgeList;
		}

		private void InitData(List<MapEdge> edgeList, MapNode.SearchType searchType, ref List<MapNode> theNodes)
		{
			Dictionary<XYZ, MapNode> dictionary = new Dictionary<XYZ, MapNode>(new XYZComparer());
			foreach (MapEdge mapEdge in edgeList)
			{
				XYZ xyz = mapEdge.StartPoint;
				if (!dictionary.ContainsKey(xyz))
				{
					MapNode value = new MapNode(xyz, new List<MapEdge>
					{
						mapEdge
					});
					dictionary.Add(xyz, value);
				}
				else
				{
					dictionary[xyz].AddEdge(mapEdge);
				}
				xyz = mapEdge.EndPoint;
				if (!dictionary.ContainsKey(xyz))
				{
					MapNode value2 = new MapNode(xyz, new List<MapEdge>
					{
						mapEdge
					});
					dictionary.Add(xyz, value2);
				}
				else
				{
					dictionary[xyz].AddEdge(mapEdge);
				}
			}
			foreach (KeyValuePair<XYZ, MapNode> keyValuePair in dictionary)
			{
				if (searchType == MapNode.SearchType._Anticlockwise)
				{
					keyValuePair.Value.AnticlockwiseSortElements();
				}
				else
				{
					keyValuePair.Value.ClockwiseSortElements();
				}
				theNodes.Add(keyValuePair.Value);
			}
		}

		private void TrimAllNode(ref List<MapNode> theNodes)
		{
			foreach (MapNode mapNode in theNodes)
			{
				int count = mapNode.EdgeList.Count;
				if (count != 1)
				{
					mapNode.AnticlockwiseSortElements();
					List<MapEdge> edgeList = mapNode.EdgeList;
					for (int i = 0; i < count; i++)
					{
						int index = i;
						int index2 = i + 1;
						if (i == count - 1)
						{
							index2 = 0;
						}
						MapEdge mapEdge = edgeList[index];
						MapEdge mapEdge2 = edgeList[index2];
						double dSAngle = 0.0;
						double dEAngle = 0.0;
						mapEdge.GetAngle(mapNode.Position, ref dSAngle);
						mapEdge2.GetAngle(mapNode.Position, ref dEAngle);
						if (!Geometry.IsEqual(Geometry.BetweenTheAngles(dSAngle, dEAngle, true), Math.PI))
						{
							this.TrimEdgeSideline(mapNode.Position, ref mapEdge, ref mapEdge2);
						}
					}
				}
			}
		}

		private void TrimEdgeSideline(XYZ position, ref MapEdge edge1, ref MapEdge edge2)
		{
			XYZ xyz = edge1.CurNegativeStartPoint(position);
			XYZ xyz2 = edge1.CurNegativeEndPoint(position);
			Curve curve;
			if (edge1.IsArcEdge)
			{
				XYZ center = edge1.Center;
				XYZ radianPnt = Geometry.CalculatMidPoint(xyz, xyz2, center, XYZ.BasisZ);
				curve = this.m_Revit.Application.ActiveUIDocument.Document.CreatYJKArc(xyz, xyz2, radianPnt);
			}
			else
			{
				curve = YJKLineEx.YJKGetBound(xyz, xyz2);
			}
			XYZ xyz3 = edge2.CurPositiveStartPoint(position);
			XYZ xyz4 = edge2.CurPositiveEndPoint(position);
			Curve curve2;
			if (edge2.IsArcEdge)
			{
				XYZ center2 = edge2.Center;
				XYZ radianPnt2 = Geometry.CalculatMidPoint(xyz3, xyz4, center2, XYZ.BasisZ);
				curve2 = this.m_Revit.Application.ActiveUIDocument.Document.CreatYJKArc(xyz3, xyz4, radianPnt2);
			}
			else
			{
				curve2 = YJKLineEx.YJKGetBound(xyz3, xyz4);
			}
			XYZ xyz5 = Geometry.IntersectWithTwoCurves(curve, curve2, position, true, true, this.m_Revit.Application.Application);
			if (xyz5 != null)
			{
				edge1.SetCurNegativeStartPoint(position, xyz5);
				edge2.SetCurPositiveStartPoint(position, xyz5);
			}
		}

		private bool SearchOuterBoundary(List<MapNode> theNodes, ref List<List<MapEdge>> outEdgeList)
		{
			int num = -1;
			int num2 = 0;
			int num3 = 0;
			int num4 = theNodes.Count * 4;
			bool isFirstPt = true;
			List<XYZ> list = new List<XYZ>();
			List<double> list2 = new List<double>();
			List<MapEdge> list3 = new List<MapEdge>();
			MapNode mapNode = null;
			MapNode prevNode = null;
			while (theNodes.Count > 1 && num2 < 2 && num3 < num4)
			{
				if (num == -1 || num == 1)
				{
					list = new List<XYZ>();
					list2 = new List<double>();
					list3 = new List<MapEdge>();
					MapNode leftBottomNode = this.GetLeftBottomNode(theNodes);
					if (leftBottomNode == null)
					{
						return false;
					}
					mapNode = leftBottomNode;
					list.Add(mapNode.Position);
					num3++;
					prevNode = null;
					isFirstPt = true;
				}
				MapEdge mapEdge = null;
				MapNode nextNode = this.GetNextNode(isFirstPt, prevNode, mapNode, theNodes, MapNode.SearchType._Anticlockwise, ref mapEdge);
				if (nextNode != null)
				{
					double item = mapEdge.CurBulge(mapNode.Position);
					list2.Add(item);
					list3.Add(mapEdge);
					prevNode = mapNode;
					isFirstPt = false;
					mapNode = nextNode;
					list.Add(mapNode.Position);
					num3++;
					if (this.IsClosed(list))
					{
						double item2 = mapEdge.CurBulge(mapNode.Position);
						list2.Add(item2);
						if (list.Count > 1)
						{
							outEdgeList.Add(list3);
						}
						if (this.m_IsSearchEdgeLoops)
						{
							return true;
						}
						this.DeleteNodeInClosedSpace(ref theNodes, list, list2);
						num = 1;
					}
					else
					{
						num = 0;
					}
					num2 = 0;
				}
				else
				{
					prevNode = null;
					isFirstPt = true;
					num2++;
					if (num2 == 2 && list.Count > 1)
					{
						outEdgeList.Add(list3);
					}
				}
			}
			return true;
		}

		private bool SearchCells(List<MapNode> theNodes, ref List<List<MapEdge>> outEdgeList)
		{
			int num = -1;
			int num2 = 0;
			int num3 = 0;
			int num4 = theNodes.Count * 4;
			bool isFirstPt = true;
			List<XYZ> list = new List<XYZ>();
			List<double> list2 = new List<double>();
			List<MapEdge> list3 = new List<MapEdge>();
			MapNode mapNode = null;
			MapNode prevNode = null;
			while (theNodes.Count > 1 && num2 < 2 && num3 < num4)
			{
				if (num == -1 || num == 1)
				{
					list = new List<XYZ>();
					list2 = new List<double>();
					list3 = new List<MapEdge>();
					MapNode leftBottomNode = this.GetLeftBottomNode(theNodes);
					if (leftBottomNode == null)
					{
						return false;
					}
					mapNode = leftBottomNode;
					list.Add(mapNode.Position);
					num3++;
					prevNode = null;
					isFirstPt = true;
				}
				MapEdge mapEdge = null;
				MapNode nextNode = this.GetNextNode(isFirstPt, prevNode, mapNode, theNodes, MapNode.SearchType._Clockwise, ref mapEdge);
				if (nextNode != null)
				{
					double item = mapEdge.CurBulge(mapNode.Position);
					list2.Add(item);
					list3.Add(mapEdge);
					prevNode = mapNode;
					isFirstPt = false;
					mapNode = nextNode;
					list.Add(mapNode.Position);
					num3++;
					if (this.IsClosed(list))
					{
						double item2 = mapEdge.CurBulge(mapNode.Position);
						list2.Add(item2);
						if (list.Count > 1)
						{
							outEdgeList.Add(list3);
						}
						this.DeletePassNode(ref theNodes, list);
						this.RecursiveDeleteSingleNode(ref theNodes);
						num = 1;
					}
					else
					{
						num = 0;
					}
					num2 = 0;
				}
				else
				{
					prevNode = null;
					isFirstPt = true;
					num2++;
					if (num2 == 2 && list.Count > 1)
					{
						outEdgeList.Add(list3);
					}
				}
			}
			return true;
		}

		private MapNode GetLeftBottomNode(List<MapNode> theNodes)
		{
			XYZ xyz = theNodes[0].Position;
			MapNode result = null;
			foreach (MapNode mapNode in theNodes)
			{
				XYZ position = mapNode.Position;
				if (position.IsAlmostEqualTo(xyz, 0.0001))
				{
					result = mapNode;
				}
				else if (this.CompareXYLess(position, xyz))
				{
					xyz = position;
					result = mapNode;
				}
			}
			return result;
		}

		private bool CompareXYLess(XYZ lhv, XYZ rhv)
		{
			if (Geometry.IsEqual(lhv.Y, rhv.Y))
			{
				if (Geometry.LessThan(lhv.X, rhv.X))
				{
					return true;
				}
			}
			else if (Geometry.LessThan(lhv.Y, rhv.Y))
			{
				return true;
			}
			return false;
		}

		private MapNode GetNextNode(bool isFirstPt, MapNode PrevNode, MapNode CurNode, List<MapNode> theNodes, MapNode.SearchType searchType, ref MapEdge nextEdge)
		{
			MapNode result = null;
			if (isFirstPt)
			{
				List<MapEdge> edgeList = CurNode.EdgeList;
				double referenceStartAngle = 3.1416926535897933;
				edgeList.Sort(new AngleComparer(CurNode.Position, referenceStartAngle));
				int i = 0;
				int num = 0;
				while (i < edgeList.Count)
				{
					MapEdge mapEdge = edgeList[i];
					int num2 = 0;
					mapEdge.GetWinding(CurNode.Position, ref num2);
					if (num2 != -1)
					{
						break;
					}
					double val = 0.0;
					mapEdge.GetAngle(CurNode.Position, ref val);
					if (!Geometry.GreaterThan(val, Math.PI) || !Geometry.Lessthan_Or_Equal(val, 4.71238898038469))
					{
						break;
					}
					num++;
					i++;
				}
				int num3 = 0;
				if (num > 0)
				{
					num3 += num;
					if (num == edgeList.Count)
					{
						num3--;
					}
				}
				nextEdge = edgeList[num3];
				nextEdge.SetIsPositiveDirection(CurNode.Position);
				XYZ pos = nextEdge.CurEndPoint(CurNode.Position);
				result = this.FindNodeByPos(theNodes, pos);
			}
			else
			{
				int num4 = CurNode.FindLinkEdgeWithOtherNode(PrevNode.Position, searchType);
				if (num4 != -1)
				{
					num4++;
					if (num4 == CurNode.EdgeList.Count)
					{
						num4 = 0;
					}
					nextEdge = CurNode.EdgeList[num4];
					nextEdge.SetIsPositiveDirection(CurNode.Position);
					XYZ pos2 = nextEdge.CurEndPoint(CurNode.Position);
					result = this.FindNodeByPos(theNodes, pos2);
				}
			}
			return result;
		}

		private MapNode FindNodeByPos(List<MapNode> theNodes, XYZ pos)
		{
			foreach (MapNode mapNode in theNodes)
			{
				if (pos.IsAlmostEqualTo(mapNode.Position, 0.0001))
				{
					return mapNode;
				}
			}
			return null;
		}

		private bool IsClosed(List<XYZ> BorderPos)
		{
			if (BorderPos.Count < 2)
			{
				return false;
			}
			XYZ xyz = BorderPos[0];
			XYZ xyz2 = BorderPos[BorderPos.Count - 1];
			return xyz.IsAlmostEqualTo(xyz2, 0.0001);
		}

		private void DeleteNodeInClosedSpace(ref List<MapNode> theNodes, List<XYZ> points, List<double> bulges)
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			List<List<double>> list2 = new List<List<double>>();
			List<EdgeInfo> list3 = new List<EdgeInfo>();
			Geometry.ConvertToSimpleClosedPolylineAndSimpleCurve(points, bulges, ref list, ref list2, ref list3);
			List<List<XYZ>> list4 = new List<List<XYZ>>();
			for (int i = 0; i < list.Count; i++)
			{
				List<XYZ> item = new List<XYZ>();
				if (Geometry.GetFittingPolyline(list[i], list2[i], ref item, Common.MMToFeet(200.0)))
				{
					list4.Add(item);
				}
			}
			List<int> list5 = new List<int>();
			List<UV> list6 = new List<UV>();
			for (int i = 0; i < theNodes.Count; i++)
			{
				XYZ position = theNodes[i].Position;
				bool flag = false;
				for (int j = 0; j < list3.Count; j++)
				{
					EdgeInfo edgeInfo = list3[j];
					if (!edgeInfo.StartPoint.IsAlmostEqualTo(edgeInfo.EndPoint, 0.0001))
					{
						if (edgeInfo.IsArc)
						{
							XYZ ptCenterOfArc = null;
							Geometry.GetCenterWithBulge(edgeInfo.StartPoint, edgeInfo.EndPoint, edgeInfo.Bulge, ref ptCenterOfArc);
							if (Geometry.Is_Point_OnSegment(edgeInfo.StartPoint, edgeInfo.EndPoint, ptCenterOfArc, XYZ.BasisZ, position))
							{
								list5.Add(i);
								list6.Add(new UV(position.X, position.Y));
								flag = true;
								break;
							}
						}
						else if (Geometry.Is_Point_OnSegment(edgeInfo.StartPoint, edgeInfo.EndPoint, position))
						{
							list5.Add(i);
							list6.Add(new UV(position.X, position.Y));
							flag = true;
							break;
						}
					}
				}
				if (!flag)
				{
					for (int j = 0; j < list4.Count; j++)
					{
						if (Geometry.PointInPloygon(list4[j], position) != -1)
						{
							list5.Add(i);
							list6.Add(new UV(position.X, position.Y));
							break;
						}
					}
				}
			}
			List<int> list7 = new List<int>();
			for (int i = 0; i < list6.Count; i++)
			{
				int num = list5[i];
				MapNode mapNode = theNodes[num];
				List<MapEdge> edgeList = mapNode.EdgeList;
				XYZ position2 = mapNode.Position;
				int k = 0;
				while (k < edgeList.Count)
				{
					XYZ xyz = edgeList[k].CurEndPoint(position2);
					UV pt2d = new UV(xyz.X, xyz.Y);
					int num2 = list6.FindIndex((UV s) => s.IsAlmostEqualTo(pt2d, 0.0001));
					if (num2 == -1)
					{
						k++;
					}
					else
					{
						MapNode mapNode2 = this.FindNodeByPos(theNodes, xyz);
						if (mapNode2 == null)
						{
							k++;
						}
						else
						{
							edgeList.RemoveAt(k);
							num2 = mapNode2.FindLinkEdgeWithOtherNode(position2, MapNode.SearchType._Anticlockwise);
							if (num2 != -1)
							{
								mapNode2.RemoveEdge(num2);
							}
						}
					}
				}
				if (edgeList.Count == 0)
				{
					list7.Add(num);
				}
				else
				{
					mapNode.AnticlockwiseSortElements();
				}
			}
			for (int i = list7.Count - 1; i >= 0; i--)
			{
				theNodes.RemoveAt(list7[i]);
			}
		}

		private void FormatEdgeLoops(List<List<MapEdge>> outEdgeList, ref List<List<MapEdge>> formatEdgeLoops)
		{
			for (int i = 0; i < outEdgeList.Count; i++)
			{
				List<MapEdge> list = outEdgeList[i];
				for (int j = 0; j < list.Count - 1; j++)
				{
					MapEdge edge = list[j];
					int num = list.FindIndex(j + 1, (MapEdge s) => s.SameAs(edge));
					if (num >= 0 && num != j)
					{
						List<MapEdge> range = list.GetRange(0, j);
						List<MapEdge> range2 = list.GetRange(j + 1, num - j - 1);
						List<MapEdge> collection = new List<MapEdge>();
						if (num < list.Count - 1)
						{
							collection = list.GetRange(num + 1, list.Count - (num + 1));
						}
						range.AddRange(collection);
						if (range.Count > 1)
						{
							outEdgeList.Add(range);
						}
						if (range2.Count > 1)
						{
							outEdgeList.Add(range2);
						}
						outEdgeList.RemoveAt(i);
						i--;
						break;
					}
				}
			}
			List<MapEdge> list2 = new List<MapEdge>();
			foreach (List<MapEdge> list3 in outEdgeList)
			{
				formatEdgeLoops.Add(list3);
				list2.AddRange(list3);
			}
			List<MapNode> list4 = new List<MapNode>();
			this.InitData(list2, MapNode.SearchType._Anticlockwise, ref list4);
			this.TrimAllNode(ref list4);
		}

		private void GetLoopCurves(List<List<MapEdge>> outEdgeList, bool isCentreLine, ref List<List<Curve>> edgeLoops)
		{
			foreach (List<MapEdge> list in outEdgeList)
			{
				List<Curve> list2 = new List<Curve>();
				foreach (MapEdge mapEdge in list)
				{
					XYZ xyz = XYZ.BasisZ;
					XYZ xyz2;
					XYZ xyz3;
					if (isCentreLine)
					{
						xyz2 = mapEdge.StartPoint;
						xyz3 = mapEdge.EndPoint;
					}
					else if (mapEdge.IsPositiveDirection)
					{
						xyz2 = mapEdge.PositiveStartPoint;
						xyz3 = mapEdge.PositiveEndPoint;
					}
					else
					{
						xyz2 = mapEdge.NegativeEndPoint;
						xyz3 = mapEdge.NegativeStartPoint;
						xyz = -xyz;
					}
					Curve item;
					if (mapEdge.IsArcEdge)
					{
						XYZ center = mapEdge.Center;
						XYZ radianPnt = Geometry.CalculatMidPoint(xyz2, xyz3, center, xyz);
						item = this.m_Revit.Application.ActiveUIDocument.Document.CreatYJKArc(xyz2, xyz3, radianPnt);
					}
					else
					{
						item = YJKLineEx.YJKGetBound(xyz2, xyz3);
					}
					list2.Add(item);
				}
				edgeLoops.Add(list2);
			}
		}

		private void DeleteSingleSideLineByBound(XYZ passInPos, XYZ passOutPos, ref List<MapNode> CurNodeAry)
		{
			for (int i = 0; i < CurNodeAry.Count; i++)
			{
				MapNode mapNode = CurNodeAry[i];
				for (int j = 0; j < mapNode.EdgeList.Count; j++)
				{
					MapEdge mapEdge = mapNode.EdgeList[j];
					XYZ startPoint = mapEdge.StartPoint;
					XYZ endPoint = mapEdge.EndPoint;
					if ((passInPos.IsAlmostEqualTo(startPoint) && passOutPos.IsAlmostEqualTo(endPoint)) || (passInPos.IsAlmostEqualTo(endPoint) && passOutPos.IsAlmostEqualTo(startPoint)))
					{
						CurNodeAry[i].EdgeList.RemoveAt(j);
					}
				}
			}
		}

		private void DeleteNodeByBound(int nodeIndex, ref List<MapNode> CurNodeAry)
		{
			List<KeyValuePair<XYZ, XYZ>> list = new List<KeyValuePair<XYZ, XYZ>>();
			for (int i = 0; i < CurNodeAry[nodeIndex].EdgeList.Count; i++)
			{
				MapEdge mapEdge = CurNodeAry[nodeIndex].EdgeList[i];
				KeyValuePair<XYZ, XYZ> item = new KeyValuePair<XYZ, XYZ>(mapEdge.StartPoint, mapEdge.EndPoint);
				list.Add(item);
			}
			for (int i = 0; i < list.Count; i++)
			{
				XYZ key = list[i].Key;
				XYZ value = list[i].Value;
				this.DeleteSingleSideLineByBound(key, value, ref CurNodeAry);
			}
			CurNodeAry.RemoveAt(nodeIndex);
		}

		private void RecursiveDeleteSingleNode(ref List<MapNode> nodeInfos)
		{
			int num = 0;
			for (int i = 0; i < nodeInfos.Count; i++)
			{
				if (nodeInfos[i].EdgeList.Count < 2)
				{
					this.DeleteNodeByBound(i, ref nodeInfos);
					i--;
					num++;
				}
			}
			if (num > 0)
			{
				this.RecursiveDeleteSingleNode(ref nodeInfos);
			}
		}

		private void DeletePassNode(ref List<MapNode> CurFloorNodeDate, List<XYZ> points)
		{
			List<int> list = new List<int>();
			List<int> list2 = new List<int>();
			for (int i = 0; i < points.Count - 1; i++)
			{
				XYZ xyz = points[i];
				for (int j = 0; j < CurFloorNodeDate.Count; j++)
				{
					XYZ position = CurFloorNodeDate[j].Position;
					if (xyz == position)
					{
						if (CurFloorNodeDate[j].EdgeList.Count > 2)
						{
							list2.Add(j);
						}
						list.Add(j);
						break;
					}
				}
			}
			int count = list2.Count;
			if (count == 1)
			{
				int index = list2[0];
				if (list.FindIndex((int value) => value == index) != -1)
				{
					list.RemoveAt(index);
				}
			}
			else if (count >= 2)
			{
				int indexStart = list2[0];
				int num = list.FindIndex((int value) => value == indexStart);
				if (num == 0)
				{
					int indexEnd = list2[1];
					int num2 = list.FindIndex((int value) => value == indexEnd);
					if (num2 != -1)
					{
						if (num2 - num == 1)
						{
							XYZ position2 = CurFloorNodeDate[num].Position;
							XYZ position3 = CurFloorNodeDate[num2].Position;
							this.DeleteSingleSideLineByBound(position2, position3, ref CurFloorNodeDate);
							this.DeleteSingleSideLineByBound(position3, position2, ref CurFloorNodeDate);
						}
						list.RemoveRange(num2, list.Count - num2);
						list.RemoveAt(num);
					}
				}
				else if (num > 0)
				{
					int indexEnd = list2[list2.Count - 1];
					int num3 = list.FindIndex((int value) => value == indexEnd);
					if (num3 != -1)
					{
						list.RemoveRange(num, num3 - num + 1);
					}
				}
			}
			if (list.Count != 0)
			{
				list.Sort();
				for (int i = list.Count - 1; i > -1; i--)
				{
					this.DeleteNodeByBound(list[i], ref CurFloorNodeDate);
				}
			}
		}

		private ExternalCommandData m_Revit;

		private bool m_IsSearchEdgeLoops;
	}
}
